package com.link.base.tencent.corpwx.accounttag.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.link.base.tencent.corpwx.accounttag.dao.mybatis.mapper.AccountTagGroupMapper;
import com.link.base.tencent.corpwx.accounttag.dao.mybatis.mapper.AccountTagItemMapper;
import com.link.base.tencent.corpwx.accounttag.model.AccountTagGroup;
import com.link.base.tencent.corpwx.accounttag.model.AccountTagItem;
import com.link.base.tencent.corpwx.accounttag.util.AccountTagUtil;
import com.link.base.tencent.corpwx.agent.service.AgentService;
import com.link.base.tencent.corpwx.agent.util.AgentIdUtil;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 客户标签
 *
 * @author 欧阳振强
 * @date 2020-01-02 15:02:09.0
 */
@Service
public class AccountTagItemServiceImpl extends BasicServiceImpl<AccountTagItem> implements AccountTagItemService {

    @Resource
    private AccountTagItemMapper accountTagItemMapper;
    @Resource
    private AccountTagGroupMapper accountTagGroupMapper;
    @Resource
    private AgentService agentService;
    
    @Override
    public BasicMapper<AccountTagItem> getBasicMapper() {
        return accountTagItemMapper;
    }

    @Override
    public void delTagItemById(AccountTagItem accountTagItem) throws Exception {
        String tagItemId = accountTagItem.getTagId();
        if (StringUtils.isNotBlank(tagItemId)) {
            // 标签Id不为空，则需要一并删除企业微信端标签
            List<String> tagList = new ArrayList<>();
            tagList.add(tagItemId);
            AccountTagUtil.delCorpTag(agentService.getExistAccessToken(AgentIdUtil.CORPWX_ACCOUNT_AGENTID), null, tagList);
        }
        // 删除CRM端标签
        accountTagItemMapper.deleteById(accountTagItem);
    }

    @Override
    public void syncTagItemToWx(AccountTagItem accountTagItem) throws Exception {
        // 获取标签对应的标签组
        AccountTagGroup accountTagGroupParam = new AccountTagGroup();
        accountTagGroupParam.setId(accountTagItem.getHeadId());
        AccountTagGroup accountTagGroup = accountTagGroupMapper.queryById(accountTagGroupParam);
        if (accountTagGroup != null) {
            // 先判断标签所属标签组是否已经同步，若未同步，则最好直接同步标签组
            String tagGroupId = accountTagGroup.getTagGroupId();
            if (StringUtils.isBlank(tagGroupId)) {
                throw new BasicServiceException("标签所属标签组尚未同步，您需要直接同步标签组！");
            }
            String tagItemId = accountTagItem.getTagId();
            if (StringUtils.isBlank(tagItemId)) {
                // 标签尚未同步，则直接新建标签
                List<AccountTagItem> accountTagItemList = new ArrayList<>();
                accountTagItemList.add(accountTagItem);
                JSONObject resultObj = AccountTagUtil.addCorpTag(agentService.getExistAccessToken(AgentIdUtil.CORPWX_ACCOUNT_AGENTID),
                    tagGroupId, accountTagItemList);
                // 比对标签名称，回写标签Id到数据库
                this.setTagItemByJSON(resultObj.getJSONObject("tag_group").getJSONArray("tag"), accountTagItemList);
            } else {
                // 已同步则更新标签
                AccountTagUtil.editCorpTag(agentService.getExistAccessToken(AgentIdUtil.CORPWX_ACCOUNT_AGENTID), tagItemId,
                    accountTagItem.getTagName(), accountTagItem.getTagOrder());
            }
        }
    }

    @Override
    public void setTagItemByJSON(JSONArray tagItemArray, List<AccountTagItem> accountTagItemList) throws Exception {
        for (int i = 0; i < tagItemArray.size(); i++) {
            JSONObject obj = tagItemArray.getJSONObject(i);
            for (AccountTagItem item: accountTagItemList) {
                if (item.getTagName().equals(obj.getString("name"))) {
                    item.setTagId(obj.getString("id"));
                    item.setCreateTime(obj.getString("create_time"));
                    item.setTagOrder(obj.getString("order"));
                    accountTagItemMapper.update(item);
                }
            }
        }
    }
}